package 题目集.dfs;

import java.util.Scanner;

/**
 * 表格计算
 * https://www.dotcpp.com/oj/problem1832.html
 */
public class Excel {
    static boolean[][] vis;
    static String[][] data;
    static double[][] finalData;
    static int n, m;

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        n = sc.nextInt();
        m = sc.nextInt();
        data = new String[n+1][m+1];
        vis = new boolean[n+1][m+1];
        finalData = new double[n+1][m+1];
        for (int i = 1; i <=n; i++) {
            for (int j = 1; j <= m; j++) {
                data[i][j] = sc.next();
                if (data[i][j].matches("^\\d")) {
                    vis[i][j] = true;
                    finalData[i][j] = Double.parseDouble(data[i][j]);
                }
            }
        }
        for (int i = 1; i <= n; i++) {
            for (int j = 1; j <= m; j++) {
                System.out.printf("%.2f ", dfs(i, j));
            }
            System.out.println();
        }
    }

    public static double dfs(int x, int y) {
        if (vis[x][y]) {
            return finalData[x][y];
        }
        double res = 0;
        String d = data[x][y].substring(3);
        String[] split = d.split(":");
        split[0] = split[0].substring(1);
        split[1] = split[1].substring(0,split[1].length() - 1);
        String[] a = split[0].split(",");
        String[] b = split[1].split(",");

        int x1 = Integer.parseInt(a[0]);
        int y1 = Integer.parseInt(a[1]);
        int x2 = Integer.parseInt(b[0]);
        int y2 = Integer.parseInt(b[1]);

        switch (data[x][y].charAt(2)) {
            case 'M':
                res = sum(x1, y1, x2, y2);
                break;
            case 'G':
                res = avg(x1, y1, x2, y2);
                break;
            case 'D':
                res = std(x1, y1, x2, y2);
                break;
        }
        vis[x][y] = true;
        finalData[x][y] = res;
        return res;
    }

    public static double sum(int x1, int y1, int x2, int y2) {
        double res=0;
        for (int i = x1; i <= x2; i++) {
            for (int j = y1; j <= y2; j++) {
                res+=dfs(i,j);
            }
        }
        return res;
    }

    public static double avg(int x1, int y1, int x2, int y2) {
        return sum(x1, y1, x2, y2) / ((x2 - x1 + 1) * (y2 - y1 + 1));
    }

    public static double std(int x1, int y1, int x2, int y2) {
        double avg = avg(x1, y1, x2, y2);
        double sum=0;
        for (int i = x1; i <= x2; i++) {
            for (int j = y1; j <= y2; j++) {
                sum+=Math.pow(dfs(i,j)-avg,2);
            }
        }
        return Math.sqrt(sum/((x2 - x1 + 1) * (y2 - y1 + 1)));
    }
}
